const express = require('express')
const router = express.Router()
const moment = require('moment')
const models = require('../../models')
const Op = models.Sequelize.Op
const {success} = require('../../utils/message')
const checkPermission = require('../../middlewares/check-permission');

// 文章数量，按月统计
router.get('/articles', checkPermission(), async function (req, res, next) {
    moment.locale('zh-cn')

    const months = []
    const data = []

    for (let i = 0; i < 12; i++) {
        let month = moment().subtract(i, 'months').format('YYYY-MM')
        months.unshift(month)

        let start = moment().subtract(i, 'months').startOf('month').format('YYYY-MM-D') + ' 00:00:00'
        let end = moment().subtract(i, 'months').endOf('month').format('YYYY-MM-D') + ' 23:59:59'

        const count = await models.Article.count({where: {'createdAt': {[Op.between]: [start, end]}}})
        data.unshift(count)
    }

    success(res, '查询成功', {months, data})
})

// 本周订单数
router.get('/sales_count', checkPermission(), async function (req, res, next) {
    moment.locale('zh-cn')
    const count = {
        create: [],
        pay: [],
        picking: [],
        shipping: [],
        finish: [],
    }
    const week_start = moment().weekday(0).format('YYYY年MM月DD日')  // 获取本周第一天
    const week_end = moment().weekday(6).format('YYYY年MM月DD日')    // 获取本周最后一天

    for (let i = 0; i < 7; i++) {
        let start = moment().weekday(i).format('YYYY-MM-DD') + ' 00:00:00'   // 本周每天开始时间
        let end = moment().weekday(i).format('YYYY-MM-DD') + ' 23:59:59'  // 本周每天结束时间

        let createCount = await models.Order.count({where: {'createdAt': {[Op.between]: [start, end]}, 'status': 1}})  // 待付款
        count.create.push(createCount)
        let payCount = await models.Order.count({where: {'paidAt': {[Op.between]: [start, end]}, 'status': 2}})        // 已付款
        count.pay.push(payCount)
        let pickingCount = await models.Order.count({where: {'pickedAt': {[Op.between]: [start, end]}, 'status': 3}})  // 配货中
        count.picking.push(pickingCount)
        let shippingCount = await models.Order.count({where: {'shippedAt': {[Op.between]: [start, end]}, 'status': 4}}) // 待收货
        count.shipping.push(shippingCount)
        let finishCount = await models.Order.count({where: {'finishedAt': {[Op.between]: [start, end]}, 'status': 5}})  // 已完成
        count.finish.push(finishCount)
    }

    success(res, '查询成功', {'week_start': week_start, 'week_end': week_end, count})
})

// 本周销售额
router.get('/sales_amount', checkPermission(), async function (req, res, next) {
    moment.locale('zh-cn')

    const amount = {
        create: [],
        pay: [],
    }

    const week_start = moment().weekday(0).format('YYYY年MM月DD日')  // 获取本周第一天
    const week_end = moment().weekday(6).format('YYYY年MM月DD日')    // 获取本周最后一天

    for (let i = 0; i < 7; i++) {
        let start = moment().weekday(i).format('YYYY-MM-DD') + ' 00:00:00'   // 本周每天开始时间
        let end = moment().weekday(i).format('YYYY-MM-DD') + ' 23:59:59'  // 本周每天结束时间

        let createSum = await models.Order.sum('totalPrice',{where: {'createdAt': {[Op.between]: [start, end]}, 'status': 1}})  // 待付款
        amount.create.push(createSum ? createSum : 0)
        let paySum = await models.Order.sum('totalPrice',{where: {'paidAt': {[Op.between]: [start, end]}, 'status': { [Op.gt]: 1 }}})  // 已付款
        amount.pay.push(paySum ? paySum : 0)
    }

    success(res, '查询成功', {'week_start': week_start, 'week_end': week_end, amount})
})


// 用户性别统计
router.get('/sexes', checkPermission(), async function (req, res, next) {
    const male = await models.User.count({where: {'sex': 1}})
    const female = await models.User.count({where: {'sex': 0}})

    const data = [
        {value: male, name: 'male'},
        {value: female, name: 'female'},
    ]

    success(res, '查询成功', {data})
})
module.exports = router
